package com.jrzh.mvc.service.sys.impl;

import java.io.Serializable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jrzh.common.exception.ProjectException;
import com.jrzh.framework.base.convert.BaseConvertI;
import com.jrzh.framework.base.repository.BaseRepository;
import com.jrzh.framework.base.service.impl.BaseServiceImpl;
import com.jrzh.framework.bean.SessionUser;
import com.jrzh.mvc.convert.sys.MobileSmsSendConvert;
import com.jrzh.mvc.model.sys.MobileSmsSendModel;
import com.jrzh.mvc.model.sys.MobileSmsSendingModel;
import com.jrzh.mvc.repository.sys.MobileSmsSendRepository;
import com.jrzh.mvc.search.sys.MobileSmsSendSearch;
import com.jrzh.mvc.service.sys.MobileSmsSendServiceI;
import com.jrzh.mvc.service.sys.manage.SysServiceManage;
import com.jrzh.mvc.view.sys.MobileSmsSendView;
import com.jrzh.notify.sms.SendSmsI;
import com.jrzh.notify.sms.bean.BaseSendBean;

@Service("mobileSmsSendService")
public class MobileSmsSendServiceImpl
		extends
		BaseServiceImpl<MobileSmsSendModel, MobileSmsSendSearch, MobileSmsSendView>
		implements MobileSmsSendServiceI {

	@Autowired
	private MobileSmsSendRepository mobileSmsSendRepository;

	@Autowired
	private SysServiceManage sysServiceManage;

	@Override
	public BaseRepository<MobileSmsSendModel, Serializable> getDao() {
		return mobileSmsSendRepository;
	}

	@Override
	public BaseConvertI<MobileSmsSendModel, MobileSmsSendView> getConvert() {
		return new MobileSmsSendConvert();
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
	@Override
	public void saveSend(SendSmsI sendSms, MobileSmsSendingModel sending,
			BaseSendBean sendBean) throws ProjectException {
		MobileSmsSendModel send = new MobileSmsSendModel();
		send.setAddTime(sending.getAddTime());
		send.setBody(sending.getBody());
		send.setIsDisable(false);
		send.setIsLock(false);
		send.setMobile(sending.getMobile());
		send.setSortNum(1);
		sysServiceManage.mobileSmsSendService.add(send, SessionUser.getSystemUser());
		sysServiceManage.mobileSmsSendingService.delete(sending.getId(), SessionUser.getSystemUser());
		sendSms.send(sendBean);
	}
}
